package com.dragon.mmochat.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.dragon.mmochat.model.enumType.MmochatBigMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyEarnType;
import com.dragon.mmochat.model.statistics.MmochatBigMoneyStatistics;
import com.dragon.mmochat.model.statistics.MmochatDailyMoneyStatistics;
import com.dragon.mmochat.model.statistics.MmochatDailyUserStatistics;
import com.dragon.mmochat.model.statistics.MmochatMonthlyStatistics;
import com.dragon.mmochat.model.statistics.MmochatRoleLevelInfo;
import com.dragon.mmochat.model.statistics.MmochatSmallMoneyStatistics;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MmochatStatisticsDao extends SimpleJdbcDaoSupport {
	private static final DailyUserMapper dailyUserMapper = new DailyUserMapper();
	private static final DailyMoneyMapper dailyMoneyMapper = new DailyMoneyMapper();
	private static final MonthlyMapper monthlyMapper = new MonthlyMapper();
	private static final SmallMoneyStatisticsMapper smallMoneyStatisticsMapper = new SmallMoneyStatisticsMapper();
	private static final BigMoneyStatisticsMapper bigMoneyStatisticsMapper = new BigMoneyStatisticsMapper();
	private static final DailyRoleLevelMapper dailyRoleLevelMapper = new DailyRoleLevelMapper();
	// 保存每日统计数据
	public static final String saveDailyStatisticsSql = "insert into day_log("
			+ "reg_num, login_num, login_num_of_yesterday_reg, online_top, online_time_average, bigmoney_pay_role_num,"
			+ "bigmoney_pay_total_money, total_reg_num, login_in_seven_day_role_num,"
			+ "total_smallmoney_rest, main_point_to_cost_smallmoney, main_point_to_produce_smallmoney,"
			+ "main_point_to_cost_bigmoney, main_point_to_produce_bigmoney, "
			+ "online_map, direct_pay_role_money_map, kb_pay_role_money_map, total_bigmoney_rest, date,"
			+ "suggest_reg_num, today_score_to_bigmoney_count, today_score_to_kb_count, "
			+ "level_info, active_level_info, today_directpay_times, today_kbpay_times, prop_use_map) "
			+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
			+ "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	public void saveDailyStatistics(int reg_num, int login_num,
			int loginNumOfYesterdayReg, int online_top,
			int online_time_average, int bigmoney_pay_role_num,
			int bigmoney_pay_total_money, int total_reg_num,
			int login_in_seven_day_role_num, long total_smallmoney_rest,
			String main_point_to_cost_smallmoney,
			String main_point_to_produce_smallmoney,
			String main_point_to_cost_bigmoney,
			String main_point_to_produce_bigmoney, String online_map,
			String direct_pay_role_money_map, String kb_pay_role_money_map,
			long total_bigmoney_rest, int suggest_reg_num,
			long today_score_to_bigmoney_count, long today_score_to_kb_count,
			String levelInfo, String activityLevelInfo,
			int todayDirectPayTimes, int todayKBPayTimes, String today_propCostPointMap) {
		getSimpleJdbcTemplate().update(saveDailyStatisticsSql, reg_num,
				login_num, loginNumOfYesterdayReg, online_top,
				online_time_average, bigmoney_pay_role_num,
				bigmoney_pay_total_money, total_reg_num,
				login_in_seven_day_role_num, total_smallmoney_rest,
				main_point_to_cost_smallmoney,
				main_point_to_produce_smallmoney, main_point_to_cost_bigmoney,
				main_point_to_produce_bigmoney, online_map,
				direct_pay_role_money_map, kb_pay_role_money_map,
				total_bigmoney_rest, new Timestamp(System.currentTimeMillis()),
				suggest_reg_num, today_score_to_bigmoney_count,
				today_score_to_kb_count, levelInfo, activityLevelInfo,
				todayDirectPayTimes, todayKBPayTimes, today_propCostPointMap);
	}

	// 保存每月统计数据
	public static final String saveMonthlyStatisticsSql = "insert into month_log("
			+ "activity_role_num, cur_month_login_num, cur_month_pay_role_num, "
			+ "cur_month_pay_total_money, date) VALUES (?, ?, ?, ?, ?)";

	public void saveMonthlyStatistics(int activity_role_num,
			int cur_month_login_num, int cur_month_pay_role_num,
			int cur_month_pay_total_money) {
		getSimpleJdbcTemplate().update(saveMonthlyStatisticsSql,
				activity_role_num, cur_month_login_num, cur_month_pay_role_num,
				cur_month_pay_total_money,
				new Timestamp(System.currentTimeMillis()));
	}

	// 查询用户日报表数据
	public static final String queryDailyUserStatisticsSql = "select date,login_num_of_yesterday_reg, reg_num, login_num, suggest_reg_num,"
			+ "online_top, online_time_average, total_reg_num, login_in_seven_day_role_num, "
			+ "online_map from day_log where date >= ? and date < ? order by date desc";

	public List<MmochatDailyUserStatistics> queryDailyUserStatistics(
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryDailyUserStatisticsSql,
				dailyUserMapper, startDate, endDate);
	}

	// 查询消费日报表数据
	public static final String queryDailyMoneyStatisticsSql = "select date, login_num,"
			+ "bigmoney_pay_total_money, bigmoney_pay_role_num, kb_pay_role_money_map,"
			+ "direct_pay_role_money_map, online_map, "
			+ "today_directpay_times, today_kbpay_times from day_log "
			+ "where date >= ? and date < ? order by date desc";

	public List<MmochatDailyMoneyStatistics> queryDailyMoneyStatistics(
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryDailyMoneyStatisticsSql,
				dailyMoneyMapper, startDate, endDate);
	}

	// 查询月报表数据
	public static final String queryMonthlyStatisticsSql = "select date, activity_role_num,"
			+ "cur_month_login_num, cur_month_pay_role_num, cur_month_pay_total_money "
			+ "from month_log where date >= ? and date < ? order by date desc";

	public List<MmochatMonthlyStatistics> queryMonthlyStatistics(
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryMonthlyStatisticsSql,
				monthlyMapper, startDate, endDate);
	}

	// 查询最近几个游戏币相关的记录
	public static final String queryLatestSmallMoneyStatisticsSql = "select date, total_smallmoney_rest,"
			+ "main_point_to_cost_smallmoney, main_point_to_produce_smallmoney"
			+ " from day_log where date >= ? and date < ? order by date";

	public List<MmochatSmallMoneyStatistics> queryLatestSmallMoneyStatistics(
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(
				queryLatestSmallMoneyStatisticsSql, smallMoneyStatisticsMapper,
				startDate, endDate);
	}

	// 查询最近几个元宝相关的记录
	public static final String queryLatestBigMoneyStatisticsSql = "select date, total_bigmoney_rest,"
			+ "main_point_to_cost_bigmoney, main_point_to_produce_bigmoney"
			+ " from day_log where date >= ? and date < ? order by date";

	public List<MmochatBigMoneyStatistics> queryLatestBigMoneyStatistics(
			Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryLatestBigMoneyStatisticsSql,
				bigMoneyStatisticsMapper, startDate, endDate);
	}

	// 查询在线分布
	public static final String queryOnlineMapSql = "select online_map from day_log "
			+ "where date >= ? and date < ? limit 1";

	public String queryOnlineMap(Timestamp startDate, Timestamp endDate) {
		return getSimpleJdbcTemplate().queryForObject(queryOnlineMapSql,
				String.class, startDate, endDate);
	}

	// 查询等级分布
	public static final String queryLevelMapSql = "select level_info, active_level_info from day_log "
			+ "where date >= ? and date < ? limit 1";

	public List<MmochatRoleLevelInfo> queryLevelMap(Timestamp startDate,
			Timestamp endDate) {
		return getSimpleJdbcTemplate().query(queryLevelMapSql,
				dailyRoleLevelMapper, startDate, endDate);
	}

	static class DailyRoleLevelMapper implements
			ParameterizedRowMapper<MmochatRoleLevelInfo> {
		@Override
		public MmochatRoleLevelInfo mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatRoleLevelInfo data = new MmochatRoleLevelInfo();

			String level_info = rs.getString("level_info");
			if (level_info != null && level_info.length() > 0) {
				String[] level_subInfo = level_info.split(",");
				if (level_subInfo != null && level_subInfo.length > 0) {
					for (String info : level_subInfo) {
						if (info.length() == 0) {
							continue;
						}
						try {
							Integer level = Integer.parseInt(info);
							if (level != null) {
								data.addLevelInfo(level);
							}
						} catch (NumberFormatException e) {
						}
					}
				}
			}

			String active_level_info = rs.getString("active_level_info");
			if (active_level_info != null && active_level_info.length() > 0) {
				String[] level_subInfo = active_level_info.split(",");
				if (level_subInfo != null && level_subInfo.length > 0) {
					for (String info : level_subInfo) {
						if (info.length() == 0) {
							continue;
						}
						try {
							Integer level = Integer.parseInt(info);
							if (level != null) {
								data.addActiveLevelInfo(level);
							}
						} catch (NumberFormatException e) {
						}
					}
				}
			}
			return data;
		}

	}

	static class DailyUserMapper implements
			ParameterizedRowMapper<MmochatDailyUserStatistics> {
		@Override
		public MmochatDailyUserStatistics mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatDailyUserStatistics data = new MmochatDailyUserStatistics();
			Timestamp stamp = rs.getTimestamp("date");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			data.setDate(df.format(stamp));
			data.setReg_num(rs.getInt("reg_num"));
			data.setLogin_num(rs.getInt("login_num"));
			data.setOnline_top(rs.getInt("online_top"));
			data.setOnline_time_average(rs.getInt("online_time_average"));
			data.setTotal_reg_num(rs.getInt("total_reg_num"));
			data.setLogin_in_seven_day_role_num(rs
					.getInt("login_in_seven_day_role_num"));
			data.setSuggest_reg_num(rs.getInt("suggest_reg_num"));
			data.setLogin_num_of_yesterday_reg(rs
					.getInt("login_num_of_yesterday_reg"));

			Gson gson = new Gson();
			String str = rs.getString("online_map");
			if (str != null) {
				Map<Long, Integer> onlineMap = gson.fromJson(str,
						new TypeToken<Map<Long, Integer>>() {
						}.getType());
				if (onlineMap != null) {
					data.setOnline_map(onlineMap);
				}
			}

			return data;
		}

	}

	static class DailyMoneyMapper implements
			ParameterizedRowMapper<MmochatDailyMoneyStatistics> {
		@Override
		public MmochatDailyMoneyStatistics mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatDailyMoneyStatistics data = new MmochatDailyMoneyStatistics();
			Timestamp stamp = rs.getTimestamp("date");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			data.setDate(df.format(stamp));
			data.setLogin_num(rs.getInt("login_num"));
			data.setBigmoney_pay_total_money(rs
					.getInt("bigmoney_pay_total_money"));
			data.setBigmoney_pay_role_num(rs.getInt("bigmoney_pay_role_num"));
			data.setTodayDirectPayTimes(rs.getInt("today_directpay_times"));
			data.setTodayKBPayTimes(rs.getInt("today_kbpay_times"));

			Gson gson = new Gson();

			String str = rs.getString("kb_pay_role_money_map");
			if (str != null) {
				Map<Integer, Integer> todayKBPayMap = gson.fromJson(str,
						new TypeToken<Map<Integer, Integer>>() {
						}.getType());
				if (todayKBPayMap != null) {
					data.setTodayKBPayMap(todayKBPayMap);
				}
			}

			str = rs.getString("direct_pay_role_money_map");
			if (str != null) {
				Map<Integer, Integer> todayDirectPayMap = gson.fromJson(str,
						new TypeToken<Map<Integer, Integer>>() {
						}.getType());
				if (todayDirectPayMap != null) {
					data.setTodayDirectPayMap(todayDirectPayMap);
				}
			}

			str = rs.getString("online_map");
			if (str != null) {
				Map<Long, Integer> onlineMap = gson.fromJson(str,
						new TypeToken<Map<Long, Integer>>() {
						}.getType());
				if (onlineMap != null) {
					data.setOnline_map(onlineMap);
				}
			}
			return data;
		}

	}

	static class MonthlyMapper implements
			ParameterizedRowMapper<MmochatMonthlyStatistics> {
		@Override
		public MmochatMonthlyStatistics mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatMonthlyStatistics data = new MmochatMonthlyStatistics();
			Timestamp stamp = rs.getTimestamp("date");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			data.setDate(df.format(stamp));
			data.setActivity_role_num(rs.getInt("activity_role_num"));
			data.setCur_month_login_num(rs.getInt("cur_month_login_num"));
			data.setCur_month_pay_role_num(rs.getInt("cur_month_pay_role_num"));
			data.setCur_month_pay_total_money(rs
					.getInt("cur_month_pay_total_money"));

			return data;
		}

	}

	static class SmallMoneyStatisticsMapper implements
			ParameterizedRowMapper<MmochatSmallMoneyStatistics> {
		@Override
		public MmochatSmallMoneyStatistics mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatSmallMoneyStatistics data = new MmochatSmallMoneyStatistics();
			Timestamp stamp = rs.getTimestamp("date");
			SimpleDateFormat df = new SimpleDateFormat("MM-dd");
			data.setDate(df.format(stamp));
			data.setTotal_smallmoney_rest(rs.getLong("total_smallmoney_rest"));

			Gson gson = new Gson();
			String str = rs.getString("main_point_to_cost_smallmoney");
			if (str != null) {
				Map<MmochatSmallMoneyCostType, Long> map = gson.fromJson(str,
						new TypeToken<Map<MmochatSmallMoneyCostType, Long>>() {
						}.getType());
				if (map != null) {
					data.setTodaySmallMoneyCostPointMap(map);
				}
			}

			str = rs.getString("main_point_to_produce_smallmoney");
			if (str != null) {
				Map<MmochatSmallMoneyEarnType, Long> map = gson.fromJson(str,
						new TypeToken<Map<MmochatSmallMoneyEarnType, Long>>() {
						}.getType());
				if (map != null) {
					data.setTodaySmallMoneyEarnPointMap(map);
				}
			}
			return data;
		}

	}

	static class BigMoneyStatisticsMapper implements
			ParameterizedRowMapper<MmochatBigMoneyStatistics> {
		@Override
		public MmochatBigMoneyStatistics mapRow(ResultSet rs, int arg1)
				throws SQLException {
			MmochatBigMoneyStatistics data = new MmochatBigMoneyStatistics();
			Timestamp stamp = rs.getTimestamp("date");
			SimpleDateFormat df = new SimpleDateFormat("MM-dd");
			data.setDate(df.format(stamp));
			data.setTotal_bigmoney_rest(rs.getLong("total_bigmoney_rest"));

			Gson gson = new Gson();
			String str = rs.getString("main_point_to_cost_bigmoney");
			if (str != null) {
				Map<MmochatBigMoneyCostType, Long> map = gson.fromJson(str,
						new TypeToken<Map<MmochatBigMoneyCostType, Long>>() {
						}.getType());
				if (map != null) {
					data.setTodayBigMoneyCostPointMap(map);
				}
			}

			str = rs.getString("main_point_to_produce_bigmoney");
			if (str != null) {
				Map<MmochatBigMoneyEarnType, Long> map = gson.fromJson(str,
						new TypeToken<Map<MmochatBigMoneyEarnType, Long>>() {
						}.getType());
				if (map != null) {
					data.setTodayBigMoneyEarnPointMap(map);
				}
			}
			return data;
		}

	}
}
